Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class SQLSportProvider
    Inherits SportsProvider

    Public Overrides Function GetSports() As System.Collections.Generic.List(Of SportDetails)
        Try
            Using con As New SqlConnection(Me.ConnectString)
                Dim cmd As New SqlCommand("af_GetSports", con)
                cmd.CommandType = CommandType.StoredProcedure
                con.Open()
                Dim reader As IDataReader = cmd.ExecuteReader
                Return GetSportCollectionFromReader(reader)
            End Using
        Catch
            Return New Generic.List(Of SportDetails)
        End Try
    End Function
    Public Overrides Function DeleteSport(ByVal sportID As Integer) As Boolean
        Try
            Using con As New SqlConnection(Me.ConnectString)
                Dim cmd As New SqlCommand("af_DeleteSport", con)
                cmd.CommandType = CommandType.StoredProcedure
                Dim pSportID As New SqlParameter("@sportId", SqlDbType.Int)
                pSportID.Value = sportID
                cmd.Parameters.Add(pSportID)
                con.Open()
                cmd.ExecuteNonQuery()
                Return True
            End Using
        Catch ex As Exception
            Return False
        End Try
    End Function
    Public Overrides Function CreateSport(ByVal Sport As SportDetails) As Integer
        Try
            Using con As New SqlConnection(Me.ConnectString)
                Dim cmd As New SqlCommand("af_CreateSport", con)
                cmd.CommandType = CommandType.StoredProcedure
                Dim pSportID As New SqlParameter("@sportId", SqlDbType.Int)
                pSportID.Direction = ParameterDirection.Output
                cmd.Parameters.Add(pSportID)
                Dim psportName As New SqlParameter("@sportName", SqlDbType.VarChar)
                psportName.Value = Sport.sportName
                cmd.Parameters.Add(psportName)
                con.Open()
                cmd.ExecuteNonQuery()

                Return Sport.ID
            End Using
        Catch
            Return 0
        End Try
    End Function
    Public Overrides Function UpdateSport(ByVal Sport As SportDetails) As Boolean
        Try
            Using con As New SqlConnection(Me.ConnectString)
                Dim cmd As New SqlCommand("af_CreateSport", con)
                cmd.CommandType = CommandType.StoredProcedure
                Dim pSportID As New SqlParameter("@sportId", SqlDbType.Int)
                pSportID.Value = Sport.ID
                cmd.Parameters.Add(pSportID)
                Dim psportName As New SqlParameter("@sportName", SqlDbType.VarChar)
                psportName.Value = Sport.sportName
                cmd.Parameters.Add(psportName)
                con.Open()
                cmd.ExecuteNonQuery()
                Return True
            End Using
        Catch ex As Exception
            Return False
        End Try
    End Function
    Public Overrides Function GetSport(ByVal SportID As Integer) As SportDetails
        Try
            Using con As New SqlConnection(Me.ConnectString)
                Dim cmd As New SqlCommand("af_GetSport", con)
                cmd.CommandType = CommandType.StoredProcedure
                Dim pSportID As New SqlParameter("@sportId", SqlDbType.Int)
                pSportID.Value = SportID
                cmd.Parameters.Add(pSportID)
                con.Open()
                Dim dr As SqlDataReader
                dr = cmd.ExecuteReader
                Return Me.GetSportFromReader(dr)
            End Using
        Catch
            Return Nothing
        End Try
    End Function

End Class
